package com.dongge.action;

import com.dongge.entity.Article;
import com.dongge.entity.Comment;
import com.dongge.service.ArticleService;
import com.dongge.service.CommentService;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by 朱东 on 2015/10/17.
 */
@Controller
@Scope("prototype")
public class CommentAction extends BaseAction<Comment> implements CURDAction{

    private Long parentId;//直接上级评论
    private Long articleId;//评论的文章
    private Long topId;//第一条评论

    @Resource
    private CommentService commentService;

    @Resource
    private ArticleService articleService;

    public String add() {
        model.setCommentTime(new Date());//设定发表评论时间
        Comment parent = commentService.get(parentId);
        Comment top    = commentService.get(topId);
        Article article = articleService.get(articleId);
        if(article != null){
            model.setArticle(article);
        }
        else {
            return "toList";
        }
        if( parent !=null ){
            model.setParent(parent);
        }
        if( top !=null ){
            model.setTop(top);
        }
        commentService.save(model);
        return "toList";
    }

    public String addPrepare() {
        if(articleId == null)//这里应该是出错的 不能反悔toList
        {
            return "toList";
        }
        Article article = articleService.get(articleId);
        if(article ==null){
            return "toList";
        }
        ActionContext.getContext().put("article", article);
        return "addPrepare";
    }

    public String delete() {

        commentService.delete(model.getId());
        return "toList";
    }

    /**
     * 修改评论内容
     * @return
     */
    public String edit() {

        Comment comment = commentService.get(model.getId());
        if(comment == null){
            return "toList";
        }
        comment.setContent(model.getContent());
        return "toList";
    }

    /**
     * 编辑评论的准备将comment放在值栈栈顶
     * @return
     */
    public String editPrepare() {
        Comment comment = commentService.get(model.getId());
        if(comment == null)
        {
            return "toList";
        }
        ActionContext.getContext().getValueStack().push(comment);
        return "editPrepare";
    }

    /**
     * 列出所有的评论放在ActionContext中#comments
     * @return
     */
    public String list() {

        List<Comment> comments = commentService.getArticleTopComments(articleId);
        ActionContext.getContext().put("comments",comments);
        Article article = articleService.get(articleId);
        ActionContext.getContext().getValueStack().push(article);

        return "list";
    }

    /**
     * 以下接收字段
     */
    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Long getArticleId() {
        return articleId;
    }

    public void setArticleId(Long articleId) {
        this.articleId = articleId;
    }

    public Long getTopId() {
        return topId;
    }

    public void setTopId(Long topId) {
        this.topId = topId;
    }
}
